package com.wenjh.jdmzt.android.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wenjh.jdmzt.AndroidKit;
import com.wenjh.jdmzt.R;
import com.wenjh.jdmzt.android.adapter.ImageListAdapter;
import com.wenjh.jdmzt.android.synctask.LoadImageTask;
import com.wenjh.jdmzt.android.view.ActionBar;
import com.wenjh.jdmzt.android.view.HandyListView;
import com.wenjh.jdmzt.android.view.PulltoRefreshListView;
import com.wenjh.jdmzt.android.view.PulltoRefreshListView.OnPullToRefreshListener;
import com.zhuamob.android.ZhuamobLayout;

public class ListPicFragment extends MainTabOptionFragment {
	private ImageListAdapter adapter = null;
	PulltoRefreshListView listView = null;
	View loadingView = null;
	View loadingImageView = null;
	private Animation mRotateAnimation;
	static final int ROTATION_ANIMATION_DURATION = 1200;
	static final Interpolator ANIMATION_INTERPOLATOR = new LinearInterpolator();
	
	
	@Override
	protected void onCreated(Bundle savedInstanceState) {
		mRotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		mRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
		mRotateAnimation.setDuration(ROTATION_ANIMATION_DURATION);
		mRotateAnimation.setRepeatCount(Animation.INFINITE);
		mRotateAnimation.setRepeatMode(Animation.RESTART);
		
		listView = (PulltoRefreshListView) findViewById(R.id.listview);
		listView.setEnableLoadMoreFoolter(false);
		listView.setLoadPolicy(HandyListView.DataLoadPolicy.Easy);
		listView.addHeaderView(AndroidKit.getLayoutInflater().inflate(R.layout.listitem_blank, null));

		adapter = new ImageListAdapter(getActivity(), listView, getParentActivity().getImageList());
		listView.setAdapter(adapter);
		
		// 加载更多的按钮
		loadingView = AndroidKit.getLayoutInflater().inflate(R.layout.include_pull_to_refreshing_header, null);
		TextView tv = (TextView) loadingView.findViewById(R.id.refresh_tv_message);
		tv.setText("加载更多...");
		loadingView.findViewById(R.id.refresh_tv_time).setVisibility(View.GONE);
		loadingView.setVisibility(View.GONE);
		loadingImageView = loadingView.findViewById(R.id.refresh_iv_processing);
		listView.addFooterView(loadingView);
		
		listView.setOnPullToRefreshListener(new OnPullToRefreshListener() {
			@Override
			public void onPullToRefresh() {
				getParentActivity().reflush();
			}
			
			@Override
			public void onLoadingViewClick() {
			}
		});
		
		listView.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				switch (scrollState) {
		        case OnScrollListener.SCROLL_STATE_IDLE:
		        	if(!adapter.isEmpty()) {
		        		adapter.notifyDataSetChanged();
		        		if(listView.getLastVisiblePosition() > adapter.getCount()-3
		        				&& !getParentActivity().isMoreLoding()) {
		        			getParentActivity().loadMore();
		        			loadingView.setVisibility(View.VISIBLE);
		        			loadingImageView.startAnimation(mRotateAnimation);
		        		}
		        	}
		            break;
		        }
			}
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
			}
		});
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				getParentActivity().onImageClick(position);
			}
		});
		
		if(adapter.isEmpty()) {
			listView.onRefreshRequested();
		}
	}
	
	@Override
	public void fillingHeader(Context context, ActionBar actionBar) {
		
	}
	
	@Override
	public void onReflushComplete() {
		if(listView.getLastVisiblePosition() >= adapter.getCount()-3) {
			loadingView.setVisibility(View.VISIBLE);
			loadingImageView.startAnimation(mRotateAnimation);
			getParentActivity().loadMore();
		}
		listView.refreshComplete();
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onLoadMoreComplete() {
		adapter.notifyDataSetChanged();
		loadingView.clearAnimation();
		loadingView.setVisibility(View.GONE);
	}
	
	@Override
	protected int getLayout() {
		return R.layout.fragment_listpic;
	}

	@Override
	public void pullToReflush() {
		listView.onRefreshRequested();
		listView.scrollToTop();
	}

	@Override
	public void refulshData() {
		if(adapter != null) {
			adapter.notifyDataSetChanged();
		}
	}
	
	@Override
	public void scrollToTop() {
		listView.scrollToTop();
	}
	
}
